<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="fn" />
    <a-button type="primary" @click="addItemList">添加事项</a-button>
    <a-list bordered :dataSource="selectedList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="stateChange(item.id)">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="remove(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ leftNumber }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="selectedKey === 1 ? 'primary' : 'default'" @click="changeKey(1)">全部</a-button>
          <a-button :type="selectedKey === 2 ? 'primary' : 'default'" @click="changeKey(2)">未完成</a-button>
          <a-button :type="selectedKey === 3 ? 'primary' : 'default'" @click="changeKey(3)">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'App',
  created () {
    // commit --> mutations
    // dispatch --> action
    this.$store.dispatch('getList')
  },
  data () {
    return {

    }
  },
  methods: {
    ...mapMutations(['setInputValue', 'addItem', 'stateChange', 'remove', 'changeKey', 'clearDone']),
    fn (e) {
      this.setInputValue(e.target.value.trim())
    },
    // 添加事项
    addItemList () {
      if (this.inputValue.length === 0) {
        this.$message.warning('输入的内容不合格')
        return
      }
      this.addItem()
    }
  },
  computed: {
    ...mapState(['list', 'inputValue', 'selectedKey']),
    ...mapGetters(['leftNumber', 'selectedList'])
  }
}
</script>

<style scoped lang="less">
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
